/*
 * Copyright (c) 2020 WildFireChat. All rights reserved.
 */

package com.cincc.cinccimdemo.ui.me.favorite.viewholder;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.common.DisplayUtils;
import com.cincc.cinccimdemo.common.audio.MediaPlayerUtils;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.CollectModel;
import com.cincc.im.model.ImResponse;
import com.cincc.im.util.Base64Util;

import org.json.JSONObject;

public class FavAudioContentViewHolder extends FavContentViewHolder {
    ImageView audioImageView;
    RelativeLayout contentLayout;
    TextView durationTextView;

    String path;

    public FavAudioContentViewHolder(@NonNull View itemView) {
        super(itemView);
        bindViews(itemView);
    }

    private void bindViews(View itemView) {
        audioImageView = itemView.findViewById(R.id.audioImageView);
        contentLayout = itemView.findViewById(R.id.audioContentLayout);
        durationTextView = itemView.findViewById(R.id.durationTextView);
    }

    @Override
    public void bind(Activity context, CollectModel item) {
        super.bind(context, item);
        try {
            String data = Base64Util.decodeToString(item.messageContent);
            JSONObject jsonObject = new JSONObject(data);
            path = jsonObject.getString("msgContent");
            int duration = jsonObject.getInt("duration");
            durationTextView.setText(duration + "''");
            ViewGroup.LayoutParams params = contentLayout.getLayoutParams();
            if (duration > Constants.TOTAL_TIME)
                duration = Constants.TOTAL_TIME;
            int increment = DisplayUtils.getScreenWidth(context) / 3 / Constants.TOTAL_TIME * duration;
            params.width = DisplayUtils.dip2px(context, 65) + increment;
            contentLayout.setLayoutParams(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onClick() {
        String targetId = TextUtils.isEmpty(favoriteItem.groupId) ? favoriteItem.fromUserId : favoriteItem.groupId;
        IMSipPhoneCtrl.getInstance().downloadImFile(favoriteItem.msgId, favoriteItem.messageType.value(), targetId, favoriteItem.imSendType, path, new ImCallback() {
            @Override
            public void callback(ImResponse imResponse) {
                if (imResponse.getCode() == 0) {
                    playAudio((String) imResponse.getData());
                }
            }
        });
    }

    private void playAudio(String path) {
        MediaPlayerUtils.getInstance().stop();
        MediaPlayerUtils.getInstance().startPlay(path);
    }
}
